package Other.LLLL;

import Other.Course.Course;

public class L {
    public static void main(String[] args) {
        Solution.longestSubstring("ababacb", 3);
    }
}

// 395. 至少有K个重复字符的最长子串

class Solution {
    public  static int longestSubstring(String s, int k) {
        return count(s, k, 0, s.length());
    }

    private static int count(String s,int k,int l,int r){
        if(r-l<k) return 0;
        int[] count = new int[26];
        for(char c : s.substring(l,r).toCharArray()){
            count[c-'a']++;
        }
        //针对大量数据的优化，跳过众多的没用的位置
        while(l<s.length()-1&&count[s.charAt(l)-'a']<k) l++;
        while(r>l&&count[s.charAt(r-1)-'a']<k) r--;

        for(int i=l;i<r;i++){
            if(count[s.charAt(i)-'a']<k){
                int ansL = count(s, k, l, i);
                int ansR = count(s, k, i+1, r);
                return Math.max(ansL, ansR);
            }
        }
        return r-l;
    }
}